<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'config.inc.php';
require_once 'common.inc.php';

session_name($conf['session_name']);
session_start();


$db = &DBUtil::connect($conf);

$controller = &new AppController();
$controller->setWorkingDir();
$controller->loadClass('SettingModel', 'setting');


$reg =& Registry::instance();
$reg->setEntry('db', $db);
$reg->setEntry('tbl_pref', $conf['tbl_pref']);
$reg->setEntry('limit', $conf['limit']);
$reg->setEntry('controller', $controller);
$reg->setEntry('conf', $conf);
$reg->setEntry('extra', $conf['extra']);


$priv = new AuthPriv();
$priv->ip = ($conf['auth_check_ip']) ? WebUtil::getIP() : '';
$priv->setSkipAuthExpired(($controller->page == 'kb_entry' && $_POST));
$priv->setSkipAuthExpired(($controller->page == 'feedback' && $_POST));
$priv->setAuthExpired($conf['auth_expired']);
//if(!$_GET) {
//	header('Location:' . APP_HOME_PATH . 'logout.php');
//	exit();
//} elseif(!$priv->isAuth()) {
if(!$priv->isAuth()) {
	header('Location:' . APP_HOME_PATH . 'logout.php?msg=auth_expired');
	exit();
} elseif ($controller->module == '' ) {
    header('Location: ' . APP_HOME_PATH . 'go.php');
    exit();
}

//$module
$priv_area = $priv->getPrivArea($controller->module, $controller->page, $controller->sub_page);
$priv->setPrivArea($priv_area);
$reg->setEntry('priv', $priv);


$nav = new AppNavigation;
$nav->setEqualAttrib('GET', $controller->getRequestKey('page'));
$nav->setSubEqualAttrib('GET', $controller->getRequestKey('sub_page'));
$nav->setGetParams(sprintf('%s=%s', $controller->getRequestKey('module'), $controller->module));
$nav->setTemplate(APP_TMPL_DIR . 'sub_menu_img.html');
$nav->setTemplateCustomRule(true);

$nav->setMenuMsg(AppMsg::getMenuMsgs(false, false, false));
$nav->setMenu($controller->module);

$extra_modules = array();
$extra_modules['question'] = array('kb_question');
foreach($extra_modules as $module => $pages) {
	if(empty($conf['extra'][$module])) {
		foreach($extra_modules[$module] as $v) {
			$nav->unsetMenuItem($v);
		}
	}
}

$menu = &$nav->generate();


// generate
require_once $controller->working_dir . 'PageController.php';
require_once 'core/app/PageRenderer.php';


$page = &new PageRenderer();
$page->template_dir = APP_TMPL_DIR;
$page->template = ($controller->getMoreParam('popup')) ? 'page_popup.html' :  'page.html';

$page->assign($conf);
$page->assign($conf['lang']);
$page->assign($view, 'content');
$page->assign($menu, 'menu');

$msg_key = ($controller->action) ? $controller->page . '_' . $controller->action : $controller->page;
$page->assign(AppMsg::hintBox($msg_key, $controller->module), 'module_msg');

$page->assign($controller->module, 'module_key');

$page->display();



if($conf['debug_info']) {
	echo getDebugInfo();
}

if($conf['debug_speed']) {
	timeprint("%min %max graf");
}

ob_end_flush();
?>